package com.functest.orm

import com.functest.framework.Assertions
import com.functest.framework.BaseFunctionalTest
import groovy.sql.Sql
import org.testng.annotations.DataProvider
import org.testng.annotations.Test

/**
 * Created with IntelliJ IDEA.
 * User: KovalevAS
 * Date: 28.06.13
 * Time: 16:19
 */
class MultiTableMapperTest extends BaseFunctionalTest {
    static {
        Class.forName('org.sqlite.JDBC');
    }
    @Test
    void MultiTableMapper_findMappers_Test() {
        //findMapper(String field)
        //find mapper by field, if there exists some mappers with the same name fields - return first
        assert TestData.multiTableMapper_without_bindings.findMapper('first') == TestData.multiTableMapper_without_bindings.mappers.get(2)
    }

    @Test
    void MultiTableMapper_getBinding_Test() {
        asTest {
            TestData.multiTableMapper_without_bindings.mappers.each {
                Assertions.weakAssertSilently "Binding is present, but ${TestData.multiTableMapper_without_bindings.getBinding(it)}", TestData.multiTableMapper_without_bindings.getBinding(it) != null
            }
        }
    }






    final static Sql mapBackDb
    static {
        mapBackDb = Sql.newInstance('jdbc:sqlite::memory:', 'org.sqlite.SQLite')
        mapBackDb.with {
            execute('create table id_table (id int)')
            execute('insert into id_table values (42)')

            execute('create table second_table (id int)')
            execute('insert into second_table values (42)')
        }
    }

    class Id {
        int id
        int second
    }

    @DataProvider
    Object[][] mapBackData() {
        SingleDbTableMapper firstMapper = new SingleDbTableMapper(
                new MapperFields().with {
                    field('id')
                    delegate as MapperFields
                },
                { mapBackDb },
                'id_table',
                'id',
                Id
        )

        SingleDbTableMapper secondMapper = new SingleDbTableMapper(
                new MapperFields().with {
                    field('id')
                    delegate as MapperFields
                }           ,
                { mapBackDb},
                'second_table',
                'id',
                Id
        )

        MultiTableMapper<Id> mapper = new MultiTableMapper<>([firstMapper, secondMapper], Id)

        Map expectedMap = mapper.queryBuilder().allFields().build().executeRaw().first()
        Map columnAliases = mapper.queryBuilder().allFields().columnAliases

        Map transformedMap = mapper.transformMap(expectedMap, columnAliases)
        Id bean = mapper.buildFromTranformed(transformedMap, Id)

        return [[mapper, bean, columnAliases, expectedMap]]
    }

    @Test(dataProvider = 'mapBackData')
    void mapBackTest(MultiTableMapper mapper, Object bean, Map columnAliases, Map expectedMap) {
        asTest {
            asStep('mapBack test') {
                Map mapBack = mapper.mapBack(bean, columnAliases)
                assert mapBack == expectedMap
            }
        }
    }

}
